const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navUrl: '',
    code: '',
    checked: true,
    activeIcon: '../../../images/auth_logo/agree_2x.png',
    inactiveIcon: '../../../images/auth_logo/no_agree_2x.png',
    userInfo: {},
    canIUseGetUserProfile: false,
  },
  onLoad: function (options) {
    let that = this;
    if (wx.getUserProfile) {
      that.setData({
        canIUseGetUserProfile: true
      })
    }
    if (wx.getStorageSync("navUrl")) {
      that.setData({
        navUrl: wx.getStorageSync("navUrl")
      })
    } else {
      that.setData({
        navUrl: '/pages/new_index/new_index'
      })
    }
  },
  goAgreement(){
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  },
  goPrivacy() {
    wx.navigateTo({
      url: '/pages/Privacy/Privacy',
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    if(!this.data.checked) {
      wx.showToast({
        title: '请先阅读并同意《用户服务协议》及《隐私政策》',
        icon: 'none'
      })
      return
    }
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        //用户按了允许授权按钮
        // wx.reLaunch({
        //   url: '/pages/new_index/new_index',
        // })
        user.loginByWeixin(res).then(res => {
          
          wx.switchTab({
            url: '/pages/new_index/new_index',
          })
        }).catch((err) => {
          console.log(err);
          //用户按了拒绝按钮
          // wx.showModal({
          //   title: '警告通知',
          //   content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
          //   success: function (res) {
          //     if (res.confirm) {
          //       wx.openSetting({
          //         success: (res) => {
          //           if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
          //             user.loginByWeixin(e.detail).then(res => {
          //               this.setData({
          //                 userInfo: res.data.userInfo
          //               });
          //               app.globalData.userInfo = res.data.userInfo;
          //               app.globalData.token = res.data.token;
          //             }).catch((err) => {
          //               console.log(err)
          //             });
          //           }
          //         }
          //       })
          //     }
          //   }
          // });
        });
      }
    })
  },

  bindGetUserInfo: function (e) {
    let that = this;
    //登录远程服务器
    if (that.data.code) {
      util.request(api.AuthLoginByWeixin, {
        code: that.data.code,
        userInfo: e.detail
      }, 'POST', 'application/json').then(res => {
        if (res.errno === 0) {
          //存储用户信息
          wx.setStorageSync('userInfo', res.userInfo);
          wx.setStorageSync('token', res.token);
          wx.setStorageSync('userId', res.userId);
        } else {
          // util.showErrorToast(res.errmsg)
          wx.showModal({
            title: '提示',
            content: res.message,
            showCancel: false
          });
        }
      });
    }
    if (that.data.navUrl && that.data.navUrl == '/pages/new_index/new_index') {
      wx.switchTab({
        url: that.data.navUrl,
      })
    } else if (that.data.navUrl) {
      wx.redirectTo({
        url: that.data.navUrl,
      })
    }
  },
  onChange() {
    this.setData({
      checked: !this.data.checked
    })
  }
})